<template>
	<view class="goods-detail-container" v-if="detaliList.goods_name">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in detaliList.pics" :key="index">
				<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">
				￥{{detaliList.goods_price}}
			</view>
			<!-- 信息主体 -->
			<view class="goods-info-body">
				<!-- 商品名字 -->
				<view class="goods-name">
					{{detaliList.goods_name}}
				</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">
				快递：免运费
			</view>
		</view>
		<rich-text :nodes="detaliList.goods_introduce"></rich-text>
		<!-- 商品导航组件区域 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	import badgeMixin from '@/mixins/tabber-badge.js'
	export default {
		mixins:[badgeMixin],
		computed: {
			...mapState("my_cart", ['cart']),
			...mapGetters('my_cart', ['total'])
		},
		data() {
			return {
				detaliList: {},
				options: [{
						icon: 'shop',
						text: '店铺',
					},
					{
						icon: 'cart',
						text: '购物车',
						info: this.total
					}
				],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			let id = options.goods_id
			this.getDeatilList(id)
		},
		watch: {
			total: {
				handler(newVal) {
					const findResult = this.options.find(x => x.text === '购物车')
					if (findResult) {
						findResult.info = newVal
					}
				},
				immediate:true
			}
			// total(newVal) {
			// 	const findResult = this.options.find(x=>x.text ==='购物车')
			// 	if(findResult) {
			// 		findResult.info = newVal
			// 	}
			// }
		},
		methods: {
			...mapMutations('my_cart', ['joinCartFun']),
			async getDeatilList(id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: id
				})
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				res.message.goods_introduce = res.message.goods_introduce.replace(/webp/g, 'jpg').replace(/<img /g,
					'<img style="display:block;"')
				this.detaliList = res.message
			},
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.detaliList.pics.map(x => x.pics_big)
				})
			},
			// 点击跳转到购物车
			onClick(e) {
				// console.log(e);
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id: this.detaliList.goods_id,
						goods_name: this.detaliList.goods_name,
						goods_price: this.detaliList.goods_price,
						goods_count: 1,
						goods_small_logo: this.detaliList.goods_small_logo,
						goods_state: true
					}
					this.joinCartFun(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #C00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		.yf {
			font-size: 12px;
			color: gray;
			margin: 10px 0;
		}
	}

	.goods-nav {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
	}

	.goods-detail-container {
		padding-bottom: 50px;
	}
</style>